package t2h.gui;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.UIManager;


public class ScreenMain implements ActionListener{

	private static final int EXIT_COMMAND = 1;
	private static final int BACK_COMMAND = 3;
	private static final int ABOUT_COMMAND = 4;
    private static final int DEMOS_COMMAND = 9;
	private static final Command exitCommand = new Command("Exit", EXIT_COMMAND);
    private static final Command backCommand = new Command("Back", BACK_COMMAND);
    private static final Command aboutCommand = new Command("About", ABOUT_COMMAND);
    static final Command showDemosCommand = new Command("Demos", DEMOS_COMMAND);
    static final Screen SCREEN = new SearchScreen();
    private Screen currentScreen;
    private static ScreenMain instance;
    private Container uiContent;
    private Container demoPanel;
    private static Form mainMenu;
    
    static ScreenMain getInstance(){
    	return instance;
    }
    
    public void startApp(){
    	instance = this;
    	setMainForm();
    }
    
    void setMainForm() {
	    Form main;
	    executeDemo();
	    if (Display.getInstance().isTablet()) {
	        main = new Form("");
	        main.setLayout(new BorderLayout());
	        demoPanel = new Container();
	        main.addComponent(BorderLayout.CENTER, demoPanel);
	        SCREEN.run(backCommand, ScreenMain.this, demoPanel);
	        main.setScrollable(false);
	        Form uiContentParent = new Form("T2H Karaoke");
	        uiContentParent.setHideInPortrait(true);
	        uiContent = uiContentParent.getContentPane();
	        uiContentParent.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
	        main.addComponent(BorderLayout.WEST, uiContentParent);
	        main.addOrientationListener(new ActionListener() {
	
	            public void actionPerformed(ActionEvent evt) {
	                Form f = (Form) demoPanel.getComponentAt(0);
	                if (Display.getInstance().isPortrait()) {
	                    f.addCommand(showDemosCommand, 1);
	                }
	                else {
	                    f.removeCommand(showDemosCommand);
	                }
	            }
	        });
	    }
	    else {
	        main = new Form("T2H Karaoke");
	
	        uiContent = main.getContentPane();
	    }
	    if (mainMenu != null) {
	        main.setTransitionInAnimator(mainMenu.getTransitionInAnimator());
	        main.setTransitionOutAnimator(mainMenu.getTransitionOutAnimator());
	    }
	    mainMenu = main;
		
	    mainMenu.addCommand(exitCommand);
	    mainMenu.setBackCommand(exitCommand);
	    mainMenu.addCommand(aboutCommand);    
	
	    mainMenu.addCommandListener(this);
	    if (Display.getInstance().getCurrent() != null) {
	        Display.getInstance().getCurrent().setTransitionOutAnimator(CommonTransitions.
	            createEmpty());
	    }
    }
    
	public void actionPerformed(ActionEvent arg0) {
		 
		// TODO Auto-generated method stub
		/*Command cmd = arg0.getCommand();
        switch (cmd.getId()) {
            case EXIT_COMMAND:
                Display.getInstance().exitApplication();
                break;
            case BACK_COMMAND:
            	currentScreen.cleanup();
                mainMenu.showBack();
                break;
            case ABOUT_COMMAND:
                Form aboutForm = new Form("About");
                aboutForm.setScrollable(true);
                aboutForm.setLayout(new BorderLayout());
                TextArea aboutText = new TextArea(getAboutText(), 5, 10);
                aboutText.setEditable(false);
                aboutForm.addComponent(BorderLayout.CENTER, aboutText);
                Command backFromAbout = new Command("Back") {

                    public void actionPerformed(ActionEvent evt) {
                        if (demoPanel != null) {
                            demoPanel.replace(demoPanel.getComponentAt(0), mainMenu, UIManager.
                                getInstance().getLookAndFeel().getDefaultFormTransitionOut().copy(
                                true));
                        }
                        else {
                            mainMenu.showBack();
                        }
                    }
                };
                aboutForm.addCommand(backFromAbout);
                aboutForm.setBackCommand(backFromAbout);
                if (demoPanel != null) {
                    demoPanel.replace(demoPanel.getComponentAt(0), aboutForm,
                        UIManager.getInstance().getLookAndFeel().getDefaultFormTransitionOut());
                }
                else {
                    aboutForm.show();
                }
                break;
            
            case DEMOS_COMMAND:
                Dialog popup = new Dialog("Demos");
                popup.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
                popup.setScrollableY(true);
                popup.showPopupDialog(arg0.getComponent());
                break;            
        }*/
	}
	
	private String getAboutText() {
        return UIManager.getInstance().localize("aboutString", "About text");
    }
	
	static void executeDemo() {
        SCREEN.run(backCommand, instance, instance.demoPanel);
    }
}
